//package com.example.springbooteasycode.utils.tablepdf;
//
//import com.aspose.words.License;
//import com.aspose.words.PdfSaveOptions;
//import com.aspose.words.SaveFormat;
//import org.apache.poi.xwpf.usermodel.*;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
//
//import java.io.*;
//import java.math.BigInteger;
//import java.util.Objects;
//import com.aspose.words.Document;
//import org.springframework.core.io.ClassPathResource;
//
//
//public class DemoLeft {
//    public static void main(String[] args) throws IOException {
//        XWPFDocument document = new XWPFDocument();
//
//        // 设置问卷基础信息表格
//        questionnaireInfoTableToDocument(document);
//
//        // 统计结果
//        questionnaireStatisticalResults(document);
//
//        // 添加单选题表格
//        addSingleChoiceTableToDocument(document);
//
//
//
//        // 输出到文件
//        try (FileOutputStream out = new FileOutputStream("/Users/boykchen/Desktop/demoLeft.docx")) {
//            document.write(out);
//        }
//
//        wordToPdf();
//
//    }
//
//    private static void questionnaireInfoTableToDocument(XWPFDocument document) {
//        // 实现问卷基础信息表格的创建
//        XWPFTable table = document.createTable(4, 2);
//
//        // 设置表格的列宽
//        table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(8300)); // 总宽度为5000
//
//        // 设置名称
//        XWPFTableRow row = table.getRow(0);
//        row.setHeight(120);
//        XWPFTableCell cell00 = row.getCell(0);
//        setTableCellStyleHead20(cell00,"名称");
//
//        XWPFTableCell cell01 =row.getCell(1);
//        setTableCellStyleContent20(cell01,"物业服务满意度2024年度调查");
//
//        // 设置时间
//        XWPFTableRow row1 = table.getRow(1);
//        row1.setHeight(120);
//        XWPFTableCell cell10 = row1.getCell(0);
//        setTableCellStyleHead20(cell10,"时间");
//
//        XWPFTableCell cell11 =row1.getCell(1);
//        setTableCellStyleContent20(cell11,"2024-10-12 16:41:06至 2024-10-14 09:12:14");
//
//        // 设置发布者
//        XWPFTableRow row2 = table.getRow(2);
//        row2.setHeight(120);
//        XWPFTableCell cell20 = row2.getCell(0);
//        setTableCellStyleHead20(cell20,"发布者");
//
//        XWPFTableCell cell21 =row2.getCell(1);
//        setTableCellStyleContent20(cell21,"新物业");
//
//        // 设置收集数
//        XWPFTableRow row3 = table.getRow(3);
//        row3.setHeight(120);
//        XWPFTableCell cell30 = row3.getCell(0);
//        setTableCellStyleHead20(cell30,"收集数");
//
//        XWPFTableCell cell31 =row3.getCell(1);
//        setTableCellStyleContent20(cell31,"4份");
//
//
//
//    }
//
//    private static void questionnaireStatisticalResults(XWPFDocument document) {
//        XWPFParagraph paragraph = document.createParagraph();
//        XWPFRun run = paragraph.createRun();
//        run.setText("统计结果");
//        // 设置字体大小为15
//        run.setFontSize(15);
//        // 字体颜色#303133
//        run.setColor("303133");
//
//        run.addBreak(); // 插入空白行
//
//        XWPFRun run2 = paragraph.createRun();
//        run2.setText("物业服务满意度2024年度调查收集已完成，共4题，有效收集份数：4份，收集时间：2024-10-12 16:41:06至2024-10-14 09:12:14");
//        // 设置字体大小为13
//        run2.setFontSize(13);
//        // 字体颜色#606266
//        run2.setColor("606266");
//    }
//
//    private static void addSingleChoiceTableToDocument(XWPFDocument document) {
//        XWPFParagraph singleChoiceTitleParagraph = document.createParagraph();
//        XWPFRun run = singleChoiceTitleParagraph.createRun();
//        run.setText("1. 卫生满意度【单选题】");
//        // 设置字体大小为15
//        run.setFontSize(15);
//        // 字体颜色#303133
//        run.setColor("303133");
//
//        // 实现单选题表格的创建
//        XWPFTable table = document.createTable(4, 3);
//
//        // common-设置表头
//        XWPFTableRow row = table.getRow(0);
//        row.setHeight(120);
//        XWPFTableCell cell00 = row.getCell(0);
//        CTTblWidth cellWidth = cell00.getCTTc().addNewTcPr().addNewTcW();
//        cellWidth.setW(BigInteger.valueOf(1000));
//        setTableCellStyleHead15(cell00,"选项");
//
//        XWPFTableRow row1 = table.getRow(0);
//        row1.setHeight(120);
//        XWPFTableCell cell10 = row1.getCell(1);
//        setTableCellStyleHead15(cell10,"小计");
//
//        XWPFTableRow row2 = table.getRow(0);
//        row2.setHeight(120);
//        XWPFTableCell cell20 = row2.getCell(2);
//        setTableCellStyleHead15(cell20,"比例");
//
//        // 设置非常满意行
//        setTableCellStyleContent15(table.getRow(1).getCell(0),"非常满意");
//        setTableCellStyleContent15(table.getRow(1).getCell(1),"0");
//        setTableCellStyleContent15(table.getRow(1).getCell(2),"0%");
//
//        // 设置满意行
//        setTableCellStyleContent15(table.getRow(2).getCell(0),"满意");
//        setTableCellStyleContent15(table.getRow(2).getCell(1),"2");
//        setTableCellStyleContent15(table.getRow(2).getCell(2),"50%");
//
//        // 设置不满意行
//        setTableCellStyleContent15(table.getRow(3).getCell(0),"不满意");
//        setTableCellStyleContent15(table.getRow(3).getCell(1),"2");
//        setTableCellStyleContent15(table.getRow(3).getCell(2),"50%");
//
//    }
//
//
//
//    /**
//     * 设置表格样式：居中
//     */
//    private static void setTableStyle(XWPFTable table) {
//        //设置表格的样式
//        table.setTableAlignment(TableRowAlign.CENTER);
//    }
//
//    private static void setTableCellStyleHead15(XWPFTableCell cell,String title){
//        setTableCellStyleHead(cell,title,15,2000);
//    }
//    private static void setTableCellStyleHead20(XWPFTableCell cell,String title){
//        setTableCellStyleHead(cell,title,20,2000);
//    }
//
//    /**
//     * 设置TableCell-表头样式
//     * @param cell
//     */
//    private static void setTableCellStyleHead(XWPFTableCell cell,String title,Integer fontSize,Integer width) {
//        // 垂直对齐
//        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//        // 设置颜色rgb值
//        cell.setColor("f7f8fa");
//        // 水平对齐
//        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
//
//        // 设置单元格宽度
//        CTTblWidth ctTblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
//        ctTblWidth.setW(BigInteger.valueOf(width));
//        //设置宽度的度量单位为 DXA，即字符单位。
//        ctTblWidth.setType(STTblWidth.DXA);
//
//        //设置单元格样式
//        XWPFParagraph paragraph = cell.getParagraphs().get(0);
//
//        if (paragraph == null) {
//            paragraph = cell.addParagraph();
//        }
//        XWPFRun run = paragraph.createRun();
//        run.setText(title);
//        // 设置字体大小
//        run.setFontSize(fontSize);
//        run.setColor("909399");
//    }
//
//    private static void setTableCellStyleContent15(XWPFTableCell cell,String value){
//        setTableCellStyleContent(cell,value,15,2000);
//    }
//    private static void setTableCellStyleContent20(XWPFTableCell cell,String value){
//        setTableCellStyleContent(cell,value,20,null);
//    }
//
//    /**
//     * 设置TableCell-内容样式
//     * @param cell
//     */
//    private static void setTableCellStyleContent(XWPFTableCell cell,String value,Integer fontSize,Integer width) {
//        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
//
//        // 设置单元格宽度
//        if (Objects.nonNull(width)){
//            CTTblWidth ctTblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
//            ctTblWidth.setW(BigInteger.valueOf(width));
//            //设置宽度的度量单位为 DXA，即字符单位。
//            ctTblWidth.setType(STTblWidth.DXA);
//        }
//
//        //设置单元格样式
//        XWPFParagraph paragraph = cell.getParagraphs().get(0);
//        if (paragraph == null) {
//            paragraph = cell.addParagraph();
//        }
//        XWPFRun run = paragraph.createRun();
//        run.setText(value);
//        // 设置字体大小为13
//        run.setFontSize(fontSize);
//    }
//
//
////    private static void wordToPdf()   {
////        String inputFilePath = "/Users/boykchen/Desktop/demoLeft.docx";
////    String outputFilePath = "/Users/boykchen/Desktop/wordToPdfDemo.pdf";
////
////        try (FileInputStream fis = new FileInputStream(inputFilePath);
////             XWPFDocument document = new XWPFDocument(fis)) {
////
////            // 创建PDF文档
////            Document pdf = new Document();
////            PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(outputFilePath));
////            pdf.open();
////
////            // 处理段落
////            for (XWPFParagraph paragraph : document.getParagraphs()) {
////                String text = paragraph.getText();
////                if (text != null && !text.trim().isEmpty()) {
////                    pdf.add(new Paragraph(text));
////                }
////            }
////
////            // 处理表格
////            for (XWPFTable table : document.getTables()) {
////                int numberOfColumns = getNumberOfColumns(table);
////                PdfPTable pdfTable = new PdfPTable(numberOfColumns);
////
////                for (XWPFTableRow row : table.getRows()) {
////                    for (XWPFTableCell cell : row.getTableCells()) {
////                        PdfPCell pdfCell = new PdfPCell();
////                        pdfCell.addElement(new Paragraph(cell.getText()));
////                        pdfTable.addCell(pdfCell);
////                    }
////                }
////                pdf.add(pdfTable);
////            }
////
////            // 关闭PDF文档
////            pdf.close();
////
////            System.out.println("Word to PDF conversion completed successfully.");
////
////        } catch (IOException | DocumentException e) {
////            e.printStackTrace();
////        }
////    }
////
////    private static int getNumberOfColumns(XWPFTable table) {
////        int maxColumns = 0;
////        for (XWPFTableRow row : table.getRows()) {
////            int columnCount = row.getTableCells().size();
////            if (columnCount > maxColumns) {
////                maxColumns = columnCount;
////            }
////        }
////        return maxColumns;
////    }
//
////    private static void wordToPdf() throws IOException, Docx4JException {
////
////        String inputFilePath = "/Users/boykchen/Desktop/demoLeft.docx";
////    String outputFilePath = "/Users/boykchen/Desktop/wordToPdfDemo.pdf";
////
////        try (InputStream is = new FileInputStream(inputFilePath)) {
////            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
////            FOSettings foSettings = Docx4J.createFOSettings();
////            foSettings.setWmlPackage(wordMLPackage);
////
////            // 设置输出流
////            try (FileOutputStream os = new FileOutputStream(outputFilePath)) {
////                Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
////            } catch (Docx4JException e) {
////                throw new RuntimeException(e);
////            }
////        }
////
////        System.out.println("转换完成！");
////    }
////    private static void wordToPdf() throws IOException {
////        //打开Word文档
////        XWPFDocument document =new XWPFDocument(new FileInputStream("/Users/boykchen/Desktop/demoLeft.docx"));
////
////
////        //创建PDF选项
////      PdfOptions options=PdfOptions.create();
////
//// //转换Word文档为PDF
////PdfConverter.getInstance().convert(docx,newFileOutputStream("output.pdf"),options);
////
////
////    }
//
//    private static void wordToPdf() throws IOException {
//
//
//        String inputFilePath = "/Users/boykchen/Desktop/demoLeft.docx";
//    String outputFilePath = "/Users/boykchen/Desktop/wordToPdfDemo.pdf";
//
//
//        try (FileInputStream fis = new FileInputStream(inputFilePath);
//             FileOutputStream fos = new FileOutputStream(outputFilePath)) {
//
//            // 设置 Aspose.Words 许可证
//            ClassPathResource resource = new ClassPathResource("license.xml");
//            InputStream licenseStream = resource.getInputStream();
//            // 设置 Aspose.Words 许可证
//            License license = new License();
//            license.setLicense(licenseStream);
//
//            // 加载Word文档
//            Document doc = new Document(fis);
//
//            // 设置PDF保存选项（可选）
//            PdfSaveOptions saveOptions = new PdfSaveOptions();
//            // 可以在这里设置各种PDF保存选项，例如压缩级别、图像质量等
//            // saveOptions.setCompression(PdfImageCompression.JPEG);
//            // saveOptions.setImageQuality(90);
//
//            // 转换为PDF并保存
//            doc.save(fos, SaveFormat.PDF);
//
//            System.out.println("Word to PDF conversion completed successfully.");
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//}
